//
//  WmcWatermarkDataTool.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/11/12.
//

import Foundation

public class WmcWatermarkDataTool {
    // Public

    /// 转换水印数据
    /// - Parameter model: 水印model
    /// - Returns: 有水印和无水印的字典
    public static func convertToWatermarkInfo(model: WmcWatermarkDetailModel) -> (watermarkInfo: [String: Any], noWatermarkInfo: [String: Any]) {
        let watermarkInfoDic: [String: Any] = model.toJSON() ?? [:]

        let watermarkInfo = WmcWatermarkDataTool.createWatermark(watermarkInfoDic, addressModel: model.staticData.drawStaticAddress, addressPrefix: model.staticData.staticAddressPrefix)
        let noWatermarkInfo = WmcWatermarkDataTool.createNoneWatermark(addressModel: model.staticData.drawStaticAddress, addressPrefix: model.staticData.staticAddressPrefix)

        let result: (watermarkInfo: [String: Any], noWatermarkInfo: [String: Any]) = (watermarkInfo: watermarkInfo.dict, noWatermarkInfo: noWatermarkInfo.dict)

        return result
    }
    
    /// 服务器时间
    /// - Returns: 时间
    public static func getNetTime() -> Date? {
        return WmcDateManager.shared.networkDate.value
    }

    // Internal

    /// 合成水印、地点和时间信息
    static func mergeInfo(_ watermarkInfo: [String: Any], addressModel: WmcSelectAddressModel?, addressPrefix: String?) -> [String: Any] {
        var result: [String: Any] = watermarkInfo

        // location是水印上的地点给用户看的，position是实际定位地点给产品自己看的

        if let poi = addressModel?.poi, let addressPrefix = addressPrefix, addressModel?.style != .customized {
            let locationDic: [String: Any] = [
                "address": poi.name ?? "",
                "cityName": poi.city ?? "",
                "district": addressPrefix,
                "latitude": "\(poi.location.latitude)",
                "longitude": "\(poi.location.longitude)",
            ]
            result["location"] = locationDic
        }

        if let location = WmcLocationManager.shared.defaultPoi?.poi,
           let addressPrefix = WmcLocationManager.shared.addressPrefix
        {
            var locationDic: [String: Any] = [
                "address": location.name ?? "",
                "cityName": location.city ?? "",
                "district": addressPrefix,
                "latitude": "\(location.location.latitude)",
                "longitude": "\(location.location.longitude)",
            ]
            result["position"] = locationDic

            if addressModel?.style == .customized {
                if let name = addressModel?.poi.name {
                    locationDic["address"] = name
                }
                result["location"] = locationDic
            }
        }

        if let date = WmcDateManager.shared.networkDate.value {
            let timeDic: [String: Any] = ["time": date.timeIntervalSince1970, "isNetTime": true]
            result["time"] = timeDic
        } else {
            let timeDic: [String: Any] = ["time": Date().timeIntervalSince1970, "isNetTime": false]
            result["time"] = timeDic
        }

        return result
    }

    static func createWatermark(_ watermarkInfo: [String: Any], addressModel: WmcSelectAddressModel?, addressPrefix: String?) -> (dict: [String: Any], json: String) {
        // 生成水印信息
        let allInfoDic = mergeInfo(watermarkInfo, addressModel: addressModel, addressPrefix: addressPrefix)

        var json = ""

        if let data = try? JSONSerialization.data(withJSONObject: allInfoDic, options: []) {
            json = String(data: data, encoding: String.Encoding.utf8) ?? ""
        }
        return (dict: allInfoDic, json: json)
    }

    static func createNoneWatermark(addressModel: WmcSelectAddressModel?, addressPrefix: String?) -> (dict: [String: Any], json: String) {
        // 生成无水印信息
        let noWatermarkInfoDic: [String: Any] = ["wmId": 0]
        let allInfoDic = mergeInfo(noWatermarkInfoDic, addressModel: addressModel, addressPrefix: addressPrefix)

        var json = ""

        if let data = try? JSONSerialization.data(withJSONObject: allInfoDic, options: []) {
            json = String(data: data, encoding: String.Encoding.utf8) ?? ""
        }
        return (dict: allInfoDic, json: json)
    }
}
